/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package calculator.manager;

import calculator.core.ICalculatorDataProvider;
import calculator.core.ICalculatorDataRenderer;
import calculator.entities.IEntitesFactory;
import calculator.entities.IMathEntity;
import calculator.entities.StringToEntityFactory;

/**
 *
 * @author Mirek
 */
public class ApplicationManager implements IApplicationManager{

    ICalculatorDataRenderer renderer;
    ICalculatorDataProvider dataProvider;
    IEntitesFactory entitesFormatter;
    
    public ApplicationManager(ICalculatorDataProvider dataProvider, ICalculatorDataRenderer dataRenderer)
    {
        entitesFormatter = new StringToEntityFactory();
        this.renderer = dataRenderer;
        this.dataProvider = dataProvider;
        
    }

    public void doAction(ICalculatorDataProvider dataProvider) {
        IMathEntity firstOperand = entitesFormatter.getEntity(dataProvider.getFirsOperandType(),
                                                              dataProvider.getFirstOperand());
        IMathEntity secondOperand = entitesFormatter.getEntity(dataProvider.getSecondOperandType(),
                                                              dataProvider.getSecondOperand());
        try{
        IMathEntity result = firstOperand.doAction(dataProvider.getOperation(), secondOperand);
        renderer.setResult(result.toString());
        }catch(Exception e){}
    }

    public void setDataProvider(ICalculatorDataProvider dataProvider) {
        this.dataProvider = dataProvider;
        dataProvider.setManager(this);
    }

    public void setDataRenderer(ICalculatorDataRenderer dataRenderer) {
        this.renderer = dataRenderer;
    }
    
}
